package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.UsuarioDao;

import model.Usuario;

/**
 * Servlet implementation class cadastroUsuario
 */
public class cadastroUsuario extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor. 
	 */
	public cadastroUsuario() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String nome = request.getParameter("nome");
		String email = request.getParameter("email");
		String senha = request.getParameter("senha");


		Usuario usuario = UsuarioDao.buscarUsuarioPorEmail(email);
		if(usuario != null){
				RequestDispatcher rd = request.getRequestDispatcher("emailRepetido.jsp");
				rd.include(request, response);
		}
		
		else{
		usuario = new Usuario();
		usuario.setNome(nome);
		usuario.setEmail(email);
		usuario.setSenha(senha);
		
		if(nome.equals("") || email.equals("") || senha.equals("")){
			RequestDispatcher rd = request.getRequestDispatcher("dadosIncompletosUsuario.jsp");
			rd.include(request, response);
		}
		else{

			UsuarioDao.adicionarUsuario(usuario);

			System.out.println("Usuario cadastrado");
			RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
			rd.include(request, response);
		}
	}
	}
}
